package com.ansion.basedb.dao.model;

import android.text.TextUtils;

import com.ansion.basedb.R;
import com.ansion.utils.ContactUtil;
import com.ansion.utils.ResourceUtils;
import com.ansion.string.StringUtils;
import com.ansion.utils.hanzi2pinyin.PinyinUtil2;


public class ContactsModel extends BaseModel {
	public static final String TAG = "ContactsModel";
	public static final String kColumnName_UserId = "userId";
	public static final String kColumnName_FirstName = "firstName";
	public static final String kColumnName_MiddleName = "middleName";
	public static final String kColumnName_LastName = "lastName";
	public static final String kColumnName_ContactId = "contactId";
	public static final String kColumnName_HasAvatarInLocalAddress = "hasAvatarInLocalAddress";
	public static final String kColumnName_SortKey = "sortKey";
	public static final String kColumnName_isArabic = "isArabic";
	public static final String kColumnName_isSymbol = "isSymbol";
	public static final int INVALID_ID = -1;

	public final static String NO_NEED_UPDATE_VALUE = "!~no_)update(_value!~";
	// 用户内部唯一号 countrycode+phone
	@DatabaseField(columnName = kColumnName_UserId, index = true, unique = true, canBeNull = false)
	private long userId;
	@DatabaseField(columnName = kColumnName_FirstName)
	private String firstName;
	@DatabaseField(columnName = kColumnName_MiddleName)
	private String middleName;
	@DatabaseField(columnName = kColumnName_LastName)
	private String lastName;
	@DatabaseField(columnName = kColumnName_ContactId)
	private int contactId;
	@DatabaseField(columnName = kColumnName_HasAvatarInLocalAddress)
	private boolean hasAvatarInLocalAddress;
	@DatabaseField(columnName = kColumnName_SortKey, index = true)
	private String sortKey;
	@DatabaseField(columnName = kColumnName_isArabic)
	private boolean isArabic;
	@DatabaseField(columnName = kColumnName_isSymbol)
	private boolean isSymbol;
	
	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public String getContactName() {
		if (!TextUtils.isEmpty(firstName) && !TextUtils.isEmpty(lastName)) {
			return firstName + " " + lastName;
		} else if (!TextUtils.isEmpty(firstName)) {
			return firstName;
		} else if (!TextUtils.isEmpty(lastName)) {
			return lastName;
		} else {
			return "";
		}
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getDisplayName() {
		return getDisplayName(true);
	}
	
	public String getDisplayName(boolean format) {
		if (StringUtils.isContainsChinese(firstName)
				|| StringUtils.isContainsChinese(lastName)
				|| StringUtils.isContainsChinese(middleName)) {
			return getChineseContactName();
		} else if (!TextUtils.isEmpty(firstName)
				&& !TextUtils.isEmpty(lastName)) {
			return firstName + " " + lastName;
		} else if (!TextUtils.isEmpty(firstName)) {
			return firstName;
		} else if (!TextUtils.isEmpty(lastName)) {
			return lastName;
		} else {
			String result = format ? ContactUtil.getFormatPhone(userId) : "+" + userId;
			return ResourceUtils.getString(R.string.bot_uid_ltr, result);
		}
	}

	private String getChineseContactName() {
		String name = "";
		if (lastName != null) {
			name += lastName;
		}
		if (middleName != null) {
			name += middleName;
		}
		if (firstName != null) {
			name += firstName;
		}
		return name;
	}

//	public String getShortName() {
//		// if (alias != null && alias.trim().length() > 0) {
//		// return getShortName(alias.trim().split(" "));
//		// } else
//		// if (nickName != null && nickName.trim().length() > 0) {
//		// return getShortName(nickName.trim().split(" "));
//		// } else
//		if (!TextUtils.isEmpty(firstName) && firstName.trim().length() > 0
//				&& !TextUtils.isEmpty(lastName) && lastName.trim().length() > 0) {
//			if (HelperFunc.isContainsChinese(firstName)
//					|| HelperFunc.isContainsChinese(lastName)) {
//				return lastName.trim().charAt(0) + "";
//			}
//			return firstName.trim().charAt(0) + "" + lastName.trim().charAt(0);
//		} else if (!TextUtils.isEmpty(firstName)
//				&& firstName.trim().length() > 0) {
//			return firstName.trim().charAt(0) + "";
//		} else if (!TextUtils.isEmpty(lastName) && lastName.trim().length() > 0) {
//			return lastName.trim().charAt(0) + "";
//		} else {
//			return "";
//		}
//	}
//
//	private String getShortName(String[] shotSplit) {
//		String b = "";
//		for (int i = 0; i < shotSplit.length; i++) {
//			if (!TextUtils.isEmpty(shotSplit[i])) {
//				b = b + shotSplit[i].charAt(0);
//			}
//			if (b.length() > 1) {
//				return b;
//			}
//		}
//		return b;
//	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public int getContactId() {
		return contactId;
	}

	public void setContactId(int contactId) {
		this.contactId = contactId;
	}

	public boolean isHasAvatarInLocalAddress() {
		return hasAvatarInLocalAddress;
	}

	public void setHasAvatarInLocalAddress(boolean hasAvatarInLocalAddress) {
		this.hasAvatarInLocalAddress = hasAvatarInLocalAddress;
	}

	public String getNameForSort() {
		// if (!TextUtils.isEmpty(nickName)) {
		// return nickName;
		// } else
		if (!TextUtils.isEmpty(firstName) && !TextUtils.isEmpty(lastName)) {
			return lastName + " " + firstName;
		} else if (!TextUtils.isEmpty(lastName)) {
			return lastName;
		} else if (!TextUtils.isEmpty(firstName)) {
			return firstName;
		} else {
			return "+" + userId;
		}
	}

	/**
	 * 
	 * @return 以英文字母的形式返回名字
	 */
	public String getSortAlpha() {
		if (StringUtils.isContainsChinese(firstName)
				|| StringUtils.isContainsChinese(lastName)
				|| StringUtils.isContainsChinese(middleName)) {
			return PinyinUtil2.hanziToPinyin(getChineseContactName(), "");
		} else if (!TextUtils.isEmpty(firstName)
				&& !TextUtils.isEmpty(lastName)) {
			return lastName + " " + firstName;
		} else if (!TextUtils.isEmpty(lastName)) {
			return lastName;
		} else if (!TextUtils.isEmpty(firstName)) {
			return firstName;
		} else {
			return ContactUtil.getFormatPhone(userId);
		}
	}

	public String getSortKey() {
		return sortKey;
	}

	public void setSortKey(String sortKey) {
		this.sortKey = sortKey;
	}
	
	public void generateSortKey(){
		sortKey = getSortAlpha();
		isArabic = StringUtils.isArabChar(sortKey.charAt(0));
		isSymbol = checkSymbol(sortKey);
	}
	
	private boolean checkSymbol(String str){
		if (str == null) {
			return true;
		}

		if (str.trim().length() == 0) {
			return true;
		}

		char firstChar = str.trim().charAt(0);

		if (StringUtils.isDigital(firstChar)) {
			return true;
		}

		if (StringUtils.isAlpha(firstChar)) {
			return false;
		}
		
		if (StringUtils.isArabChar(firstChar)) {
			return false;
		}	

		String c = PinyinUtil2.hanziToPinyin(str);
		if (!TextUtils.isEmpty(c)) {
			c = c.trim();
			if (c.length() > 0) {
				firstChar = c.charAt(0);
				if (StringUtils.isAlpha(firstChar)) {
					return false;
				}
			}else{
				return true;
			}
		}
		return true;
	}
	public boolean getIsArabic() {
		return isArabic;
	}

	public void setIsArabic(boolean isArabic) {
		this.isArabic = isArabic;
	}

	public boolean isSymbol() {
		return isSymbol;
	}

	public void setSymbol(boolean isSymbol) {
		this.isSymbol = isSymbol;
	}
	
	
}
